package com.cskaoyan.market.controller;/*
 *文件名: AdminKeywordController
 *创建者:wxin
 *创建时间:2024/5/6 21:54
 */

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.AdminKeywordService;
import com.cskaoyan.market.service.AdminKeywordServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;


@WebServlet("/admin/keyword/*")
public class AdminKeywordController extends HttpServlet {
    private AdminKeywordService keywordService = new AdminKeywordServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("create".equals(op)) {
            create(req, resp);
        } else if ("update".equals(op)) {
            update(req, resp);
        } else if ("delete".equals(op)) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        if (id == null) {
            Object fail = ResponseUtil.fail();
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        int result = keywordService.logicalDelete(id);
        if (result==0){
            Object fail = ResponseUtil.fail();
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        Object ok = ResponseUtil.ok();
        String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(jsonStr);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        if (StringUtils.isEmpty(keyword)) {
            Object fail = ResponseUtil.fail(404, "关键词不能为空");
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketKeyword marketKeyword = objectMapper.readValue(requestBody, MarketKeyword.class);
        MarketKeyword resultMarketKeyword = keywordService.updataKeyword(marketKeyword);
        HashMap<String, Object> data = new HashMap<>();
        data.put("id", resultMarketKeyword.getId());
        data.put("addTime", resultMarketKeyword.getAddTime());
        data.put("isDefault", resultMarketKeyword.getIsDefault());
        data.put("isHot", resultMarketKeyword.getIsHot());
        data.put("keyword", resultMarketKeyword.getKeyword());
        data.put("updateTime", resultMarketKeyword.getUpdateTime());
        data.put("url", resultMarketKeyword.getUrl());
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        if (StringUtils.isEmpty(keyword)) {
            Object fail = ResponseUtil.fail(404, "关键词不为空");
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketKeyword marketKeyword = objectMapper.readValue(requestBody, MarketKeyword.class);
        MarketKeyword resultMarketKeyword = keywordService.addKeyword(marketKeyword);
        HashMap<String, Object> data = new HashMap<>();
        data.put("id", resultMarketKeyword.getId());
        data.put("addTime", resultMarketKeyword.getAddTime());
        data.put("isDefault", resultMarketKeyword.getIsDefault());
        data.put("isHot", resultMarketKeyword.getIsHot());
        data.put("keyword", resultMarketKeyword.getKeyword());
        data.put("updateTime", resultMarketKeyword.getUpdateTime());
        data.put("url", resultMarketKeyword.getUrl());
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));


    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);

        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(object);
            resp.getWriter().println(s);
            return;
        }
        List<MarketKeyword> adminKeywordList = keywordService.list(page, limit, keyword, url, sort, order);

        Object o = ResponseUtil.okList(adminKeywordList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }

}
